/* 

Paper: Gentrification and pioneer businesses 
Authors: Behrens, Boulam, Martin, Mayneris 

Name dofile: identify_gentri_blocks_philly.do  
Version: 16 nov. 2021 

Output: gentri_dum_id_conc_philly.dta, blocks_gentrified_philly.dta, blocks_couldgentrify_philly.dta

Inputs:

- id_conc_database_philly.dta (from create_db_concordedblocks_philly.do)
*/ 



use id_conc_database_philly.dta, clear
tsset id_conc t
g sh_edu=edu_college_block/pop if pop!=. 
rename percap_income_block pc

keep id_conc pc sh_edu year pop

sum pop if pop>0,d 
drop if pop<8 

reshape wide sh_edu pc pop, i(id_conc) j(year)  

// compute blocks decile - by year 

foreach i in 1990 2000 2010{
local z 10 // # of bins 
xtile pctile_pc`i'=pc`i', n(`z')
xtile pctile_sh_edu`i'=sh_edu`i', n(`z')
}

foreach i in sh_edu pc {
g change_decile_`i'_9000=pctile_`i'2000 - pctile_`i'1990
g change_decile_`i'_0010=pctile_`i'2010 - pctile_`i'2000
g gr_`i'_9000=`i'2000/`i'1990 - 1
g gr_`i'_0010=`i'2010/`i'2000 - 1
}

foreach i in 9000 0010 {
label var change_decile_sh_edu_`i' "Decile change, share of educated, period `i' "
label var change_decile_pc_`i' "Decile change, per capita income, period `i' "
label var gr_sh_edu_`i' "Growth rate, share of educated, period `i' "
label var gr_pc_`i' "Growth rate, per capita income, period `i' "
}

foreach i in 1990 2000 2010{
label var pop`i' "Population, year `i'"
label var sh_edu`i' "Share of college educated, year `i'"
label var pc`i' "Per capita income, year `i'"
}


* Def. finale: below 6th pctile and an increase in the share of educated
g gentri_9000=(pctile_pc1990<6 & change_decile_pc_9000>2 & change_decile_pc_9000!=. ) & (change_decile_sh_edu_9000>0) // 1.78% 
g gentri_0010=(pctile_pc2000<6 & change_decile_pc_0010>2  & change_decile_pc_0010!=.) & (change_decile_sh_edu_0010>0) // 2.10% 

foreach i in 9000 0010{
egen gr_sh_edu_med_`i'=mean(gr_sh_edu_`i') 
egen gr_sh_pc_med_`i'=mean(gr_pc_`i') 
}


tab gentri_9000
tab gentri_0010


preserve 
g poor_9000=(pctile_pc1990<6   )  
g poor_0010=(pctile_pc2000<6 )   
g yes=poor_9000+ poor_0010
drop if yes==0
keep id_conc poor_9000 poor_0010 pctile_pc1990 pctile_pc2000
sort id_conc
saveold blocks_couldgentrify_philly, replace v(13)
restore 


preserve 
g yes=gentri_9000+ gentri_0010
drop if yes==0
keep id_conc gentri_9000 gentri_0010 pctile_pc1990 pctile_pc2000 pop*
compress 
sort id_conc 
saveold blocks_gentrified_philly, replace v(13)

use blocks_gentrified_philly, clear
duplicates drop id_conc, force
g yes1990=(gentri_9000==1) 
g yes2000= (gentri_0010==1)

keep if yes1990==1|yes2000==1
keep id_conc yes1990 yes2000
rename yes1990 gentri1990
rename yes2000 gentri2000
saveold temp, replace v(13)


use id_conc_database_philly.dta, clear
duplicates drop id_conc, force
merge 1:1 id_conc using temp
 
replace gentri1990=0 if _m==1
replace gentri2000=0 if _m==1

keep id_conc gentri1990 gentri2000 intplat intplon

saveold gentri_dum_id_conc_philly.dta, v(13) replace

erase temp.dta 

